package com.guiji.quartz.domain;

import cn.edu.hfut.dmic.contentextractor.ContentExtractor;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.wanghaomiao.xpath.model.JXDocument;
import com.guiji.quartz.config.ProxyConfig;
import com.guiji.quartz.task.DataSaveUtil;
import com.guiji.quartz.util.DelPicUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * @program: cms-vue-plus
 * @description:
 * @author: gaoX
 * @date 2021/12/28 18:38
 */
public class Trends {
	private static final Logger log = LoggerFactory.getLogger(Trends.class);

	public static void mainMethod() throws Exception {
		HttpResponse response = null;
		try {
			response = HttpRequest.get("https://trends.google.com/trends/api/dailytrends?hl=en_US&tz=-480&geo=US&ns=15")
				.header("Connection", "keep-alive")
				.header("Accept-Encoding", "gzip, deflate, br")
				.setHttpProxy(ProxyConfig.url, ProxyConfig.ip)
				.setConnectionTimeout(15000)
				.execute();
		} catch (Exception e) {
			log.error("代理失败1" + e);
			return;
		}
		String resp = response.body().substring(6);
		JSONObject jsonObject = JSONUtil.parseObj(resp);
		JSONObject aDefault = jsonObject.get("default", JSONObject.class);
		JSONArray jsonArray = aDefault.get("trendingSearchesDays", JSONArray.class);
		JSONObject jsonObject1 = jsonArray.get(0, JSONObject.class);
		JSONArray jsonArray1 = jsonObject1.get("trendingSearches", JSONArray.class);
		ArrayList<Map<String, String>> list = new ArrayList();
		jsonArray1.forEach(jo -> {
			Map<String, String> map = new HashMap();
			JSONObject news = JSONUtil.parseObj(jo);
			JSONObject title = news.get("title", JSONObject.class);
			String description = title.get("query", String.class);
			JSONObject image = news.get("image", JSONObject.class);
			String newsUrl = image.get("newsUrl", String.class);
			map.put("description", description);
			map.put("newsUrl", newsUrl);
			list.add(map);
		});
		getContent(list, "daily-Hotnews");

	}

	public static void mainMethod2() throws Exception {
		mainMethodAll("https://trends.google.com/trends/api/realtimetrends?hl=en_US&tz=-480&cat=b&fi=0&fs=0&geo=US&ri=300&rs=20&sort=0", "enterprise-Hotnews");
	}

	public static void mainMethodM() throws Exception {
		mainMethodAll("https://trends.google.com/trends/api/realtimetrends?hl=en_US&tz=-480&cat=m&fi=0&fs=0&geo=US&ri=300&rs=20&sort=0", "health-Hotnews");
	}

	public static void mainMethodH() throws Exception {
		mainMethodAll("https://trends.google.com/trends/api/realtimetrends?hl=en_US&tz=-480&cat=h&fi=0&fs=0&geo=US&ri=300&rs=20&sort=0", "focus-Hotnews");
	}

	public static void mainMethodT() throws Exception {
		mainMethodAll("https://trends.google.com/trends/api/realtimetrends?hl=en_US&tz=-480&cat=t&fi=0&fs=0&geo=US&ri=300&rs=20&sort=0", "tech-Hotnews");
	}

	public static void mainMethodS() throws Exception {
		mainMethodAll("https://trends.google.com/trends/api/realtimetrends?hl=en_US&tz=-480&cat=s&fi=0&fs=0&geo=US&ri=300&rs=20&sort=0", "sport-Hotnews");
	}

	public static void mainMethodE() throws Exception {
		mainMethodAll("https://trends.google.com/trends/api/realtimetrends?hl=en_US&tz=-480&cat=e&fi=0&fs=0&geo=US&ri=300&rs=20&sort=0", "entertainment-Hotnews");
	}

	public static void mainMethodAll(String url, String category) throws Exception {

		HttpResponse response = null;
		try {
			response = HttpRequest.get(url)
				.header("Connection", "keep-alive")
				.header("Accept-Encoding", "gzip, deflate, br")
				.setHttpProxy(ProxyConfig.url, ProxyConfig.ip)
				.setConnectionTimeout(30000)
				.execute();
		} catch (Exception e) {
			log.error("代理失败1" + e);
			return;
		}
		String resp = response.body().substring(5);
		JSONObject jsonObject = JSONUtil.parseObj(resp);
		JSONObject aDefault = jsonObject.get("storySummaries", JSONObject.class);
		JSONArray jsonArray = aDefault.get("trendingStories", JSONArray.class);
		ArrayList<Map<String, String>> list = new ArrayList();
		jsonArray.forEach(jo -> {
			Map<String, String> map = new HashMap();
			JSONObject news = JSONUtil.parseObj(jo);
			String description = news.get("title", String.class);
			JSONObject image = news.get("image", JSONObject.class);
			String newsUrl = image.get("newsUrl", String.class);
			map.put("description", description);
			map.put("newsUrl", newsUrl);
			list.add(map);
		});
		getContent(list, category);

	}

	private static void getContent(ArrayList<Map<String, String>> list, String category) throws InterruptedException {
		for (Map<String, String> map : list) {
			Thread.sleep(10000);
			String url = map.get("newsUrl");
			String description = map.get("description");
			log.info("getContent执行到-{},listContent长度-{}", url, list.size());
			String doc;
			JXDocument jxDocument;
			try {
				HttpResponse response = HttpRequest.get(url).setHttpProxy(ProxyConfig.url, ProxyConfig.ip).setConnectionTimeout(15000).execute();
				doc = response.body();
				jxDocument = new JXDocument(doc);
			} catch (Exception e) {
				log.info("trends-代理失败2" + e);
				continue;
			}
			String title = null;
			try {
				title = (String) jxDocument.sel("//h1/text()").get(0);
				if(title.length()<2){
					title = (String) jxDocument.sel("//h1/text()").get(1);
				}
			} catch (Exception e) {
				log.error("trends-标题错误-------------" + url + "-------------------标题错误");
				continue;
			}
			String content = null;
			try {
				content = ContentExtractor.getContentByHtml(doc.replaceAll("</p>", "\\$\\$\\$</p>").replaceAll("</ul>", "\\$\\$\\$</ul>")
					.replaceAll("</li>", "\\$\\$\\$</li>"));
			} catch (Exception e) {
				log.error("trends-内容错误");
				continue;
			}

			List<String> newImgs = new ArrayList<>();
			try {
				newImgs = DelPicUtil.delPic(jxDocument.sel("//head//meta[@property='og:image']/@content"));
			} catch (Exception e) {
				e.printStackTrace();
			}

			Date date = new Date();

			if (content.length() > 50000 || content.toString().replaceAll("\\$|[\\s\\p{Zs}]", "").length() < 20 || newImgs.size() > 20) {
				continue;
			}
			if(category.contains("entertainment-Hotnews")){
				int i = RandomUtil.randomInt(1, 3);
				category = "entertainment-Hotnews-"+i;
			}
			try {
				DataSaveUtil.saveData1(title, content, newImgs, url, date, "https://trends.google.com/", category, description);
				log.info(Thread.currentThread().getName() + "-----------------{}----------------", category);
			} catch (Exception e) {
				continue;
			}

		}
	}

//	public static void main(String[] args) throws Exception {
////		ArrayList<Map<String,String>> list = new ArrayList<>();
////		Map<String,String> map = new HashMap<>();
////		map.put("newsUrl","https://www.coventrytelegraph.net/news/uk-world-news/gut-wrenching-lotto-winners-who-22479885");
////		map.put("description","asdasd");
////		list.add(map);
////		getContent(list,"aaaaa");
//		mainMethod2();
//	}
}
